package com.woniuxy.micropurchaseserver.suppliermsg.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.woniuxy.micropurchaseserver.bean.SupplierBean;
import com.woniuxy.micropurchaseserver.suppliermsg.service.SupplierService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Resource
    private SupplierService supplierService;
    @RequestMapping(value = "/inc",produces = {"application/json;charset=utf-8"})
    public Long IncSupplier(SupplierBean supplierBean){
        System.out.println(supplierBean);
        Long i = supplierService.InsertSupplier(supplierBean);
        return i;
    }
    @RequestMapping(value = "/find",produces = {"application/json;charset=utf-8"})
    public List<SupplierBean> FindSupplier(SupplierBean supplierBean){
        List<SupplierBean> supplierBeans = supplierService.findAllSupplier(supplierBean);
        return supplierBeans;
    }
    @RequestMapping(value = "/del",produces = {"application/json;charset=utf-8"})
    public Integer DelSupplier(Integer id){
        Integer i = supplierService.deleteSupplierById(id);
        return i;
    }
    @RequestMapping(value = "/update",produces = {"application/json;charset=utf-8"})
    public Integer UpdateSupplier(SupplierBean supplierBean){
        System.out.println(supplierBean);
        Integer i = supplierService.UpdateSupplier(supplierBean);
        return i;
    }
    @RequestMapping(value = "/export",produces = {"application/json;charset=utf-8"})
    public Integer ExportSupplier(SupplierBean supplierBean){
        System.out.println(supplierBean);
        List<SupplierBean> supplierBeans = supplierService.findAllSupplier(supplierBean);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("供应商信息表","供应商信息"),SupplierBean.class,supplierBeans);

        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("D:/supplier.xls");
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
